
project , uses(../data_output/legislators_klarner.dta)

	**********
	** Texas House	
	** No term limits
	** First year: 	
	**********
	
	
	
	
	use ../data_output/legislators_klarner.dta, clear
	keep if state=="TX" & chamber=="house"
	keep if regexm(term, "1989|1991|1993|1995|1997|1999|2001|2003|2005|2007|2009|2011|2013|2015")
	gen lastname = trim(regexs(1)) if regexm(lower(CandName),"([^,]+)")
	gen firstname = trim(regexs(1)) if regexm(lower(CandName),",( [^ ]+)")
	gen mergename = lastname  
	bys chamber mergename term: gen n = _n
	by chamber mergename term: egen mean_n=mean(n)
	replace mergename = lastname + ", " + firstname if mean_n>1  //firstname  + " " +  lastname //firstname+ ". "
	drop n mean_n	
	save ../data_output/legislators_TX_house_1989_2016.dta, replace
project, creates(../data_output/legislators_TX_house_1989_2016.dta)		
	
	
	
project, original(../data_input/TX/tx_bills.csv)

	import delimited using ../data_input/TX/tx_bills.csv,  clear  delimiters(";") bindquotes(strict) encoding("utf-8") varnames(1)
	gen term = ""
	replace term = "2015-2016" if regexm(session,"84")
	replace term = "2013-2014" if regexm(session,"83")
	replace term = "2011-2012" if regexm(session,"82")
	replace term = "2009-2010" if regexm(session,"81")	
	replace term = "2007-2008" if regexm(session,"80")
	replace term = "2005-2006" if regexm(session,"79")
	replace term = "2003-2004" if regexm(session,"78")
	replace term = "2001-2002" if regexm(session,"77")
	replace term = "1999-2000" if regexm(session,"76")
	replace term = "1997-1998" if regexm(session,"75")
	replace term = "1995-1996" if regexm(session,"74")
	replace term = "1993-1994" if regexm(session,"73")
	replace term = "1991-1992" if regexm(session,"72")
	replace term = "1989-1990" if regexm(session,"71")
	
	gen chamber = ""
	replace chamber = "house" if regexm(bill, "^H")
	replace chamber = "senate" if regexm(bill, "^S")
	keep if chamber=="house"
	gen mergename = lower(sponsor)
	replace mergename = subinstr(mergename, `"""',"",.)
	replace mergename = subinstr(mergename, "'","",.)
	replace mergename = subinstr(mergename, "-","",.)	
	replace mergename = "gonzalez" if mergename=="gonzalez, naomi" & regexm(term, "2011")
	replace mergename = "lewis, gibson" if mergename=="lewis, gib" & regexm(term, "1989|1991")
	
	replace mergename = "hudson, samuel" if mergename=="hudson, sam" & regexm(term, "1989")
	replace mergename = "hudson" if mergename=="hudson, sam" & regexm(term, "1991")
	replace mergename = "edwards" if mergename=="edwards, al" & regexm(term, "1989|1991")
	replace mergename = "evans" if mergename=="evans, larry" & regexm(term, "1989|1991")
	replace mergename = "hallowell" if mergename=="hollowell" & regexm(term, "1989")
	replace mergename = "jones" if mergename=="jones, delwin" & regexm(term, "1989|1991|2007")
	replace mergename = "luna, albert" if mergename=="luna, al" & regexm(term, "1989")
	replace mergename = "luna" if mergename=="luna, vilma" & regexm(term, "1995|1997|1999|2001")
	replace mergename = "luna" if mergename=="luna, gregory" & regexm(term, "1991")
	replace mergename = "luna" if mergename=="hernandez luna" & regexm(term, "2013")
	replace mergename = "moreno, alejandro" if mergename=="moreno, alex" & regexm(term, "1989")
	replace mergename = "parker" if mergename=="parker, jim" & regexm(term, "1989|1991")
	replace mergename = "delagarza" if mergename=="de la garza" & regexm(term, "1991|1993|1995")
	replace mergename = "harris" if mergename=="harris, jack" & regexm(term, "1991|1993|1995")
	replace mergename = "kubiak" if mergename=="kubiak, dan" & regexm(term, "1991")
	replace mergename = "moreno" if mergename=="moreno, paul" & regexm(term, "1991|2007")
	replace mergename = "moreno, joe" if mergename=="moreno, joe e." & regexm(term, "2001|2003|2005")
	replace mergename = "turner, robert" if mergename=="turner, bob" & regexm(term, "1993|1995|1997|1999|2001")	
	replace mergename = "turner" if mergename=="turner, sylvester" & regexm(term, "1991")
	replace mergename = "vandeputte" if mergename=="van de putte" & regexm(term, "1991|1993|1995|1997|1999")
	replace mergename = "vondohlen" if mergename=="von dohlen" & regexm(term, "1991")
	replace mergename = "patterson" if regexm(mergename, "patterson") & regexm(term, "1993|1995|1997")
	replace mergename = "west" if regexm(mergename, "west, george") & regexm(term, "1993|1995|1997|1999|2001|2003|2005|2007")	
	replace mergename = "nixon" if regexm(mergename, "nixon, joe") & regexm(term, "1995|1997|1999|2001")
	replace mergename = "galloway" if mergename=="galloway, carolyn" & regexm(term, "1997")
	replace mergename = "keel" if mergename=="keel, terry" & regexm(term, "1997")
	replace mergename = "crownover" if regexm(mergename, "crownover") & regexm(term, "1999|2001")
	replace mergename = "ellis" if regexm(mergename, "ellis") & regexm(term, "1999|2001|2003")
	replace mergename = "martinezfischer" if regexm(mergename, "martinez fischer") & regexm(term, "2001|2003|2005|2007|2009|2011|2013|2015")
	replace mergename = "solis" if regexm(mergename, "solis") & regexm(term, "2001")
	replace mergename = "villarreal" if regexm(mergename, "villarreal, mike") & regexm(term, "2001")
	replace mergename = "cook, robert" if regexm(mergename, "cook, robby") & regexm(term, "2003|2005|2007")
	replace mergename = "keffer, james" if regexm(mergename, "keffer, jim") & regexm(term, "2003|2005")
	replace mergename = "keffer" if regexm(mergename, "keffer, jim") & regexm(term, "2007")
	replace mergename = "vanarsdale" if regexm(mergename, "van arsdale") & regexm(term, "2003|2005|2007")
	replace mergename = "corte" if regexm(mergename, "corte, frank") & regexm(term, "2005|2007|2009")
	replace mergename = "howard" if regexm(mergename, "howard, charlie") & regexm(term, "2005")
	replace mergename = "isett" if regexm(mergename, "isett, carl") & regexm(term, "2005|2007")
	replace mergename = "jackson" if regexm(mergename, "jackson, jim") & regexm(term, "2005|2007|2009|2011")
	replace mergename = "martinez" if regexm(mergename, "martinez, mando") & regexm(term, "2005|2007|2009|2011|2013|2015")
	replace mergename = "noriega" if regexm(mergename, "noriega, rick") & regexm(term, "2005|2007")
	replace mergename = "toureilles" if regexm(mergename, "gonzalez toureilles") & regexm(term, "2005|2007|2009")
	replace mergename = "allen" if regexm(mergename, "allen, alma") & regexm(term, "2007")	
	replace mergename = "king, tracy" if regexm(mergename, "king, tracy o.") & regexm(term, "2007|2009|2011|2013|2015")
	replace mergename = "lucio" if regexm(mergename, "lucio iii") & regexm(term, "2007|2009|2011|2013|2015")
	replace mergename = "mallorycaraway" if regexm(mergename, "mallory caraway") & regexm(term, "2007|2009|2011")
	replace mergename = "ortiz" if regexm(mergename, "ortiz, jr.") & regexm(term, "2007|2009")
	replace mergename = "taylor" if regexm(mergename, "taylor") & regexm(term, "2009|2013")
	replace mergename = "ybarra" if regexm(mergename, "rios ybarra") & regexm(term, "2009")
	replace mergename = "anderson, charles" if regexm(mergename, "anderson, charles") & regexm(term, "2011|2015")
	replace mergename = "hernandez" if regexm(mergename, "hernandez luna") & regexm(term, "2011")
	replace mergename = "munoz" if regexm(mergename, "munoz, jr.") & regexm(term, "2011")
	replace mergename = "patrick" if regexm(mergename, "patrick, diane") & regexm(term, "2011|2013")
	replace mergename = "rodriguez" if regexm(mergename, "rodriguez, eddie") & regexm(term, "2011")
	replace mergename = "gonzales" if regexm(mergename, "gonzales, larry") & regexm(term, "2013|2015")
	replace mergename = "menendez" if regexm(mergename, "menndez") & regexm(term, "2013|2015")
	replace mergename = "munoz" if regexm(mergename, "muoz, jr.") & regexm(term, "2013|2015")
	replace mergename = "nevarez" if regexm(mergename, "nevrez") & regexm(term, "2013|2015")
	replace mergename = "pena" if regexm(mergename, "pea") & regexm(term, "2015")
	replace mergename = "sheffield, j." if regexm(mergename, "sheffield, j. d.") & regexm(term, "2013")
	replace mergename = "johnson" if regexm(mergename, "johnson, eric") & regexm(term, "2015")
	replace mergename = "romero" if regexm(mergename, "romero, jr.") & regexm(term, "2015")
	replace mergename = "gonzalez, mary" if regexm(mergename, "gonzlez, mary") & regexm(term, "2013")
	replace mergename = "gonzalez" if regexm(mergename, "gonzlez, mary") & regexm(term, "2015")
	replace mergename = "marquez" if regexm(mergename, "mrquez") & regexm(term, "2013|2015")
		
	
	merge m:1 term mergename using ../data_output/legislators_TX_house_1989_2016.dta
	*14 observations do not merge
	drop if _merge==1
	drop _merge	
	save ../data_output/bills_by_sponsor_TX_1989_2016.dta, replace
project, creates(../data_output/bills_by_sponsor_TX_1989_2016.dta)		
	
	
	use ../data_output/bills_by_sponsor_TX_1989_2016.dta, clear
	
	drop sponsor
	gen termlimited=0
	gen sponsor = 1 if bill!=""
	collapse (sum) sponsor (first) elections_* terms_in_office win TermLengthActual CandName district chamber state party mergename inoffice year termlimited vtsh, by(CandId term )	
	
	save ../data_output/total_bills_by_sponsor_TX_1989_2016.dta, replace
project, creates(../data_output/total_bills_by_sponsor_TX_1989_2016.dta)		
	
		